package cn.ting.mybatis.session.impl;

import cn.ting.mybatis.executor.Executor;
import cn.ting.mybatis.po.Configuration;
import cn.ting.mybatis.po.User;
import cn.ting.mybatis.proxy.MapperProxyFactory;
import cn.ting.mybatis.session.SqlSession;

import java.lang.reflect.Proxy;
import java.util.List;

/**
 * 〈功能简述〉<br> 
 * 〈〉
 *
 * @author ting
 */
public class MySqlSession implements SqlSession {
    private Configuration configuration;
    private Executor executor;
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }




    public Configuration getConfiguration() {
        return configuration;
    }



    public void setExecutor(Executor executor) {
        this.executor = executor;
    }


    public void setConfiguration(Configuration configuration) {
        this.configuration = configuration;
    }
    /**
     * 使用动态代理技术获取代理对象
     * @param mapperClass
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public <T> T getMapper(Class<?> mapperClass) {
        //newProxyInstance()里面需要传入三个参数:
        // classLoader类加载器,
        // 代理对象实现的接口的字节码对象的数组，
        // InvocationHandler接口的实现类对象
        //获取动态代理类的代理对象实例
        return (T) Proxy.newProxyInstance(mapperClass.getClassLoader(),new Class[]{mapperClass},new MapperProxyFactory(this));
    }



    public  <T> List<T> selectList(String Key){
        //真正执行查询
        return executor.selectList(Key);

    }

    /**
     * 查询一个
     * */
    public Object selectOne(String Key,Object parameter){

        //真正执行查询
        return executor.query(Key,parameter);

    }

    /**
     * 删除一个
     * */
    public int deleteOne(String Key,Object parameter){

        //真正执行删除
        return executor.delete(Key,parameter);


    }

    /**
     * 更新一个
     * */
    public int updateOne(String Key,String parameter){
        //真正执行删除
        return executor.update(Key,parameter);

    }



    public void close() {

    }
}
